<template>
    <el-dialog v-model="visible" title="新建膳食日历" width="500px" :close-on-click-modal="false"
        class="add-meal-calendar-dialog">
        <el-form :model="form" :rules="rules" ref="formRef" label-width="80px" class="form-body">
            <el-form-item label="膳食名" prop="name">
                <el-select v-model="form.name" placeholder="请选择膳食名" style="width: 100%">
                    <el-option v-for="item in mealitemList" :key="item.mealitemId" :label="item.name"
                        :value="item.name" />
                </el-select>
            </el-form-item>

            <el-form-item label="星期" prop="day">
                <el-select v-model="form.day" placeholder="请选择星期" style="width: 100%">
                    <el-option v-for="item in weekdays" :key="item" :label="item" :value="item" />
                </el-select>
            </el-form-item>

            <el-form-item label="餐次" prop="meal">
                <el-select v-model="form.meal" placeholder="请选择餐次" style="width: 100%">
                    <el-option v-for="item in meals" :key="item" :label="item" :value="item" />
                </el-select>
            </el-form-item>
        </el-form>

        <template #footer>
            <div class="dialog-footer">
                <el-button @click="visible = false">取消</el-button>
                <el-button type="primary" @click="handleSubmit">确认</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup>
import { ref, defineExpose, defineEmits } from 'vue'
import {inject} from 'vue'
const axios = inject("axios")
import { ElMessage } from 'element-plus'

const visible = ref(false)
const formRef = ref()
const emit = defineEmits(['success'])

const form = ref({
    name: '',
    day: '',
    meal: ''
})

const rules = {
    name: [{ required: true, message: '请选择膳食名', trigger: 'change' }],
    day: [{ required: true, message: '请选择星期', trigger: 'change' }],
    meal: [{ required: true, message: '请选择餐次', trigger: 'change' }]
}

const mealitemList = ref([])
const weekdays = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
const meals = ['早餐', '午餐', '晚餐']

const open = () => {
    visible.value = true
    resetForm()
    fetchMealitems()
}

const resetForm = () => {
    form.value = {
        name: '',
        day: '',
        meal: ''
    }
}

const fetchMealitems = async () => {
    try {
        const res = await axios.post('/mealitem/getMealitem')
        mealitemList.value = res.data || []
    } catch (err) {
        ElMessage.error('获取膳食信息失败')
    }
}

const handleSubmit = () => {
    formRef.value.validate(async (valid) => {
        if (!valid) {
            ElMessage.warning('请填写完整信息')
            return
        }
        try {
            const res = await axios.post('/mealSchedule/addMealSchedule', null, {
                params: form.value
            })
            if (res.data.status === 200) {
                ElMessage.success('添加成功')
                visible.value = false
                emit('success')
            } else {
                ElMessage.error(res.data.msg || '添加失败')
            }
        } catch (err) {
            ElMessage.error('请求失败')
        }
    })
}

defineExpose({ open })
</script>

<style scoped>
.add-meal-calendar-dialog ::v-deep(.el-dialog__header) {
    padding-bottom: 10px;
}

.form-body {
    padding: 10px 0;
}

.el-form-item {
    margin-bottom: 20px;
}

.dialog-footer {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    padding-top: 10px;
}

.el-input,
.el-select {
    width: 100%;
}
</style>
